package com.tuo51.childcare.controller;


import com.alibaba.fastjson.JSON;
import com.tuo51.childcare.domain.vo.DailyGrowRecordVO;
import com.tuo51.childcare.service.DailyService;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/papi/daily")
@Api(value = "/papi/daily", tags = "家长服务号 - 每日相关")
public class PAPIDailyController {

    @Autowired
    MemVerificationCache cache;

    @Autowired
    private DailyService dailyService;

    @RequestMapping(value = "/task", method = RequestMethod.GET)
    public StatusResponse dailyTask(@RequestHeader(value = "ticket", required = false) String ticket) {

        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(dailyService.dailyTask(staff));
    }

    @RequestMapping(value = "/finish_task", method = RequestMethod.POST)
    public StatusResponse finishTask(@RequestHeader(value = "ticket", required = false) String ticket, String taskId) {
        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(dailyService.finishTask(staff, taskId));
    }

    @RequestMapping(value = "/task_templates", method = RequestMethod.POST)
    public StatusResponse taskTemplates(@RequestHeader(value = "ticket", required = false) String ticket, String reportId) {
        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(dailyService.taskTemplates(staff, reportId));
    }

    @RequestMapping(value = "/save_plan", method = RequestMethod.POST)
    public StatusResponse savePlan(@RequestHeader(value = "ticket", required = false) String ticket,
                                   @RequestParam(value = "templateIds") String templateIds) {
        Staff staff = cache.getStaff(ticket);

        String[] ids = templateIds.split("\\|");

        List<String> param = Arrays.asList(ids);

        return StatusResponse.success(dailyService.savePlan(staff, param));
    }

    @RequestMapping(value = "/grow_record", method = RequestMethod.GET)
    @ApiOperation("获取成长记录模板")
    public StatusResponse dailyGrowRecord(@RequestHeader(value = "ticket", required = false) String ticket) {

        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(dailyService.dailyGrowRecordTemplates(staff));
    }

    @RequestMapping(value = "/save_grow_record", method = RequestMethod.POST)
    @ApiOperation("保存成长记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "ticket", required = true, dataType = "string"),
            @ApiImplicitParam( name = "observers", value = "记录数据JSON字符串，[{\"title\":\"标题\",\"value\":\"值\",\"dailyIndex\":0,\"templateId\":\"dddd\"}]", required = true, dataType = "string"),
    })
    public StatusResponse saveGrowRecord(@RequestHeader(value = "ticket", required = false) String ticket,
                                         @RequestParam(value = "observers") String observersStr) {

        Staff staff = cache.getStaff(ticket);

        List<DailyGrowRecordVO> observers = JSON.parseArray(observersStr, DailyGrowRecordVO.class);

        return StatusResponse.success(dailyService.saveGrowRecord(staff, observers));
    }
}
